package battleships;

import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletResponse;

@WebFilter({ "*.css", "*.js", "*.png" })
public class CachingFilter implements Filter {

	@Override
	public void destroy() {
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		HttpServletResponse resp = (HttpServletResponse)response;
		Calendar inOneYearExpires = new GregorianCalendar();
        inOneYearExpires.add(Calendar.YEAR, 1);
        resp.setDateHeader("Expires", inOneYearExpires.getTimeInMillis());
        resp.setDateHeader("Last-Modified", (new Date()).getTime());
        //resp.setHeader("Cache-Control", "public");
        chain.doFilter(request, response);
		
	}

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
	}

}
